package stsj.meddroid.screen;

import stsj.meddroid.MainAppListener;
import stsj.meddroid.Resources;
import stsj.meddroid.Settings;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class StoryScreen implements Screen, InputProcessor {

	public int storyNumber;
	private int storyRightBorder[];

	private MainAppListener mainListener;

	private SpriteBatch batch;

	private Animation henshin;

	private Camera camera;

	private float animationTime;
	
	private TextureRegion medal;
	
	private final TextureRegion storyEnding;

	int x;
	int y;

	private static final int leftBorder = 240;

	private int scrollingSpeed = 1;
	private boolean moving;

	private long startDelay;

	public int getRightBorder() {
		return storyRightBorder[storyNumber];
	}

	public StoryScreen(MainAppListener mainListener, int storyNumber) {
		// TODO Auto-generated constructor stub
		this.mainListener = mainListener;
		this.storyNumber = storyNumber;
		storyNumber = 1;
		
		
		storyRightBorder = new int[8];
		storyRightBorder[1] = 480*3 +10;
		storyRightBorder[2] = 960;
		storyRightBorder[3] = 960;
		storyRightBorder[4] = 960;
		storyRightBorder[5] = 960;
		storyRightBorder[6] = 960;
		storyRightBorder[7] = 2048;
		
		henshin = new Animation(0.25f, Resources.henshin);
		

		storyEnding = Resources.story[7];

		x = leftBorder;
		y = Resources.VP_HEIGHT / 2;

		camera = Resources.camera;
		camera.position.set(x, y, 0);

		batch = Resources.batch;

	}

	@Override
	public void dispose() {

	}

	@Override
	public void hide() {

	}

	@Override
	public void pause() {

	}

	@Override
	public void render(float arg0) {

		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		if (moving) {
			if (x <= storyRightBorder[storyNumber] - 240) {
				x += scrollingSpeed;
			} else {
				startDelay = System.currentTimeMillis();
				moving = false;
			}
		}

		batch.begin();
		switch (storyNumber) {
		case 1: {
			batch.draw(Resources.story[1], 0, 0);
			break;
		}
		case 2: {
			batch.draw(Resources.story[2], 0, 0);
			break;
		}
		case 3: {
			batch.draw(Resources.story[3], 0, 0);
			break;
		}
		case 4: {
			batch.draw(Resources.story[4], 0, 0);
			break;
		}
		case 5: {
			batch.draw(Resources.story[5], 0, 0);
			break;
		}
		case 6: {
			batch.draw(Resources.story[6], 0, 0);
			break;
		}
		case 7 : {
			batch.draw(storyEnding, 0, 0);
//			if (Settings.getResult() == 3)
//				batch.draw(Resources.medal[3], 2048-100, 160);
//			else if (Settings.getResult() == 2)
//				batch.draw(Resources.medal[2], 2048-100, 160);
//			else if (Settings.getResult() == 1)
//				batch.draw(Resources.medal[1], 2048-100, 160);
//				
			batch.draw(medal, 2048-100, 160 - medal.getRegionHeight());
			break;
		}

		}
		

		batch.end();
		
		if (!moving) {
			if (System.currentTimeMillis() - startDelay >= 1000) {
				System.out.println(System.currentTimeMillis());
				if (storyNumber > 1 && storyNumber < 6) {
					startBattle(1);
				} else if (storyNumber == 1) {
					
					TextureRegion frame = henshin.getKeyFrame(animationTime, true);
					batch.begin();
					batch.draw(frame, 240 + 480*2 - 60, 160 - 40);
					
					batch.end();
					
					if (System.currentTimeMillis() - startDelay >= 2700){
						startBattle(1);
					}
					
					animationTime+= Gdx.graphics.getDeltaTime();
				}else if (storyNumber == 6) {
					startBattle(5);
				} else if (storyNumber == 7) {
					if (System.currentTimeMillis() - startDelay >= 6000){
						mainListener.setState(MainAppListener.CREDIT);
					}
					
				}
			}
		}

		camera.position.set(x, y, 0);
		camera.update();
		batch.setProjectionMatrix(camera.combined);

		
	}

	@Override
	public void resize(int arg0, int arg1) {

	}

	@Override
	public void resume() {

	}

	@Override
	public void show() {
		Gdx.input.setCatchBackKey(false);
		Gdx.input.setInputProcessor(this);
		Resources.stopAllMusics();
		moving = true;
		x = leftBorder;
		if (storyNumber != 7)
			Resources.playMusic(Resources.bgmStory);
		else{
			
			medal = Resources.starMedal[Settings.getResult()];
			Resources.playMusic(Resources.bgmEnding);
			
		}

		Gdx.input.setInputProcessor(this);
		System.out.println(System.currentTimeMillis());
	}

	@Override
	public boolean keyDown(int arg0) {

		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {

		return false;
	}

	@Override
	public boolean keyUp(int arg0) {

		if (arg0 == Input.Keys.BACK) {
			// Resources.playSound(Resources.backSound);
			// Resources.playMusic(Resources.bgmOpening);
			// switch (storyNumber){
			// case 1:{
			// setState(MainAppListener.STAGE_1_ROUND);
			// break;
			// }
			// case 2:{
			// setState(MainAppListener.STAGE_2_ROUND);
			// break;
			// }
			// case 3:{
			// setState(MainAppListener.STAGE_3_ROUND);
			// break;
			// }
			// case 4:{
			// setState(MainAppListener.STAGE_4_ROUND);
			// break;
			// }
			// case 5:{
			// setState(MainAppListener.STAGE_5_ROUND);
			// break;
			// }
			//			
			// }
			return false;
		}
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {

		return false;
	}

	@Override
	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {

		return false;
	}

	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {

		return false;
	}

	@Override
	public boolean touchMoved(int arg0, int arg1) {

		return false;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		if (storyNumber != 7)
			moving = false;
		return false;
	}

	private void setState(int state) {
		mainListener.setState(state);
	}

	public void startBattle(int round) {
		if (storyNumber != 6) {
			if (Settings.roundsUnlocked[storyNumber][round])
				mainListener.startBattle(storyNumber, round, this);
		} else {
			if (Settings.roundsUnlocked[5][round])
				mainListener.startBattle(5, round, this);
		}
	}

	private void startLoading(int round) {
		Screen lastScreen = null;
		lastScreen = this;
		// if (storyNumber == 1){
		// ((RoundSelectScreen)lastScreen).currentStage = 1;
		// }else if (storyNumber == 2){
		// ((RoundSelectScreen)lastScreen).currentStage = 2;
		// }else if (storyNumber == 3){
		// ((RoundSelectScreen)lastScreen).currentStage = 3;
		// }else if (storyNumber == 4){
		// ((RoundSelectScreen)lastScreen).currentStage = 4;
		// }else if (storyNumber == 5){
		// ((RoundSelectScreen)lastScreen).currentStage = 5;
		// }
		Screen loading = new LoadingScreen(lastScreen, round);
		mainListener.setScreen(loading);
	}

}
